﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.views;
using System.Security;
using com.eforceglobal.crux.security;
using com.eforceglobal.crux.DTOs;
using System.Web;
using System.Web.Security;


namespace com.eforceglobal.crux.controllers
{
    public class LoginController
    {
        ILogin _view = null;
        public LoginController(ILogin view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if(!IsPostBack)
                _view.AccessDenied = false;
        }

        public void Login()
        {
            ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            SessionData sessionData = new SessionData();
            bool AccessGranted = cruxSecurityManager.AuthenticateUser(_view.UserName, _view.Password);
            bool AccessDenied = !AccessGranted;
            _view.AccessDenied = AccessDenied;
            if (AccessGranted)
            {
                CompanyInformation companyInformation = cruxSecurityManager.GetCompayByUser(_view.UserName);
                sessionData.CurrentUser = cruxSecurityManager.GetUserFromCompany(_view.UserName, companyInformation.CompanyName);
                FormsAuthentication.RedirectFromLoginPage(_view.UserName, false);
            }

            
            
        }
    }
}
